/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OptionsDialog.java
 *
 * Created on 10.07.2010, 18:13:13
 */
package ru.ibrae.mchs.radiation.GUI;

import ru.ibrae.mchs.radiation.entities.Pokazatel;
import ru.ibrae.mchs.radiation.entities.SubjectRf;
import ru.ibrae.mchs.radiation.entities.controllers.PokazatelJpaController;
import ru.ibrae.mchs.radiation.entities.controllers.SubjectRfJpaController;
import ru.ibrae.mchs.radiation.entities.controllers.exceptions.PreexistingEntityException;
import java.awt.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBox;
import org.jdesktop.swingx.VerticalLayout;

/**
 *
 * @author murr
 */
public class OptionsDialog extends javax.swing.JDialog {

    private File configFile;

    /** Creates new form OptionsDialog */
    public OptionsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        jXPanel1.setLayout(new VerticalLayout());
        loadAll(new File("conf/config.xml"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jXTitledSeparator1 = new org.jdesktop.swingx.JXTitledSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("ОК");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Отмена");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jXPanel1.setName("jXPanel1"); // NOI18N

        jXTitledSeparator1.setTitle("Показатели:");
        jXTitledSeparator1.setName("jXTitledSeparator1"); // NOI18N

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
                .addContainerGap())
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(385, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jXPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jXPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        PokazatelJpaController pjc = new PokazatelJpaController();
        ArrayList<String> pokazatelsArray = new ArrayList<String>();
        for (int i = 0; i < pjc.findPokazatelEntities().size(); i++) {
            Pokazatel pokazatel = pjc.findPokazatelEntities().get(i);
            for (Component component : jXPanel1.getComponents()) {
                if (component instanceof JCheckBox) {
                    JCheckBox jCheckBox = (JCheckBox) component;
                    if (jCheckBox.isSelected() && pokazatel.getPokazatel().equalsIgnoreCase(jCheckBox.getText())) {
                        pokazatelsArray.add(pokazatel.getIdP().toString());
                        break;
                    }
                }
            }
            String itog = "";
            for (int j = 0; j < pokazatelsArray.size(); j++) {
                String string = pokazatelsArray.get(j);
                if (j == 0) {
                    itog = string;
                } else {
                    itog += ";" + string;
                }
            }
            Properties p = new Properties();
            try {
                p.loadFromXML(new FileInputStream(new File("conf/config.xml")));
            } catch (IOException ex) {
                Logger.getLogger(OptionsDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
            p.setProperty("pokazateli", itog);
            try {
                p.storeToXML(new FileOutputStream(new File("conf/config.xml")), "conf/config.xml");
            } catch (IOException e) {
                System.err.println(e.getMessage());
            }
            setVisible(false);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                OptionsDialog dialog = new OptionsDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    /**
     * @return the configFile
     */
    public File getConfigFile() {
        return configFile;
    }

    private void loadAll(File config) {
        PokazatelJpaController pjc = new PokazatelJpaController();
        Properties p = new Properties();
        try {
            p.loadFromXML(new FileInputStream(config));
        } catch (IOException ex) {
            Logger.getLogger(OptionsDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        String checkedPokazatels = p.getProperty("pokazateli");
        String[] checkedPokazatel = checkedPokazatels.split(";");
        for (Pokazatel pokazatel : pjc.findPokazatelEntities()) {
            JCheckBox box = new JCheckBox(pokazatel.getPokazatel());
            box.setSelected(false);
            for (String string : checkedPokazatel) {
                if (Integer.parseInt(string) == pokazatel.getIdP()) {
                    box.setSelected(true);
                    break;
                }
            }
            jXPanel1.add(box);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator1;
    // End of variables declaration//GEN-END:variables
}
